package cn.lijigou.shopping.service;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.location.Location;
import android.telephony.TelephonyManager;
import android.util.Log;
import cn.lijigou.shopping.domain.API;
import cn.lijigou.shopping.domain.Advertising;
import cn.lijigou.shopping.domain.CityTag;
import cn.lijigou.shopping.domain.MLocation;
import cn.lijigou.shopping.domain.MResult;
import cn.lijigou.shopping.domain.MRsa;
import cn.lijigou.shopping.domain.Order;
import cn.lijigou.shopping.domain.Push;
import cn.lijigou.shopping.domain.Shop;
import cn.lijigou.shopping.domain.User;
import cn.lijigou.shopping.domain.Website;
import cn.lijigou.shopping.util.HttpUtil;

public class ServerService {
	public final static int TIMEOUT = 5;

	public static MResult open(Context context) {
		TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		String sn = tm.getDeviceId();
		
		MSystem.last_start_time = Math.round(new Date().getTime()/1000);
		SharedPreferences sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
		String last_end_time = sp.getString("last_end_time", "");
		String last_start_time = sp.getString("last_start_time", "");
		String pv = sp.getString("pv", "");
		String buy = sp.getString("buy", "");
		String detail = sp.getString("detail", "");
		String edit = sp.getString("edit", "");
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		//添加要传递的参数
		params.add(new BasicNameValuePair("sn",sn));
		params.add(new BasicNameValuePair("market",API.market));
		params.add(new BasicNameValuePair("pv",pv));
		params.add(new BasicNameValuePair("buy",buy));
		params.add(new BasicNameValuePair("detail",detail));
		params.add(new BasicNameValuePair("end_time",last_end_time));
		params.add(new BasicNameValuePair("start_time", last_start_time));
		params.add(new BasicNameValuePair("edit", edit));

		MResult result = HttpUtil.POST(API.getAPI(API.open)+"&sn="+sn,params);
		if(result!=null){
			Editor editor = sp.edit();
			editor.putString("last_end_time","");
			editor.putString("last_start_time","");
			editor.putString("pv","");
			editor.putString("buy","");
			editor.putString("detail","");
			editor.putString("edit","");
			editor.commit();
		}
		return result;
	}
	public static MResult location_stat(String lat,String lng,String city,String address) {
		try {
			city = URLEncoder.encode(city, "utf-8");
			address = URLEncoder.encode(address, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer(API.getAPI(API.user_location));
		sb.append("&lng=").append(lng);
		sb.append("&lat=").append(lat);
		sb.append("&city=").append(city);
		sb.append("&address=").append(address);
		MResult mResult = HttpUtil.GET(sb.toString());
		return mResult;
	}
	public static MResult searchShops(List<Shop> shops, String lat, String lng,
			int page, String keyword, String city) {

		int start = shops.size();
		try {
			city = URLEncoder.encode(city, "utf-8");
			keyword = URLEncoder.encode(keyword, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer(API.getAPI(API.search_shop));
		sb.append("&lng=").append(lng);
		sb.append("&lat=").append(lat);
		sb.append("&city=").append(city);
		sb.append("&q=").append(keyword);
		sb.append("&page=").append(page);
		sb.append("&gz=1");
		sb.append("&offset=").append("8");

		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String title = (String) item.getString("title");
					String info = (String) item.getString("info");
					String sort1 = (String) item.getString("sort");
					String distance = (String) item.getString("distance");
					String shop_id = (String) item.getString("shop_id");
					String shop_title = (String) item.getString("shop_title");
					String shop_address = (String) item
							.getString("shop_address");
					String shop_tel = (String) item.getString("shop_tel");
					String shop_phone = (String) item.getString("shop_phone");
					String shop_sort1 = (String) item.getString("shop_sort");
					String area_id = (String) item.getString("area_id");
					String area_name = (String) item.getString("area_name");
					String area_img = (String) item.getString("area_img");

					String value = (String) item.getString("value");
					String price = (String) item.getString("price");
					String expireTime = (String) item.getString("expireTime");
					String discount = (String) item.getString("discount");
					String detailed_sort = (String) item
							.getString("detailed_sort");
					String rating = (String) item.getString("rating");
					String use_restriction = (String) item
							.getString("use_restriction");
					String use_time = (String) item.getString("use_time");
					String end_time = (String) item.getString("end_time");
					String use_num = (String) item.getString("use_num");
					String source = (String) item.getString("source");
					String buy_url = (String) item.getString("buy_url");
					String photo_small = (String) item.getString("photo_small");
					String photo_large = (String) item.getString("photo_large");
					String is_buy = (String) item.getString("is_buy");
					String mlat = (String) item.getString("latitude");
					String mlng = (String) item.getString("longitude");
					String poi_id = (String) item.getString("poi_id");
					int cpsState = (int) item.getInt("is_cps");
					int buy_start_time = (int)item.getInt("buy_start_time");
					int buy_end_time = (int)item.getInt("buy_end_time");
					String detail = (String)item.getString("tuan_detail");
					Shop shop = new Shop(id, title, info, sort1, distance,
							shop_id, shop_title, shop_address, shop_tel,
							shop_phone, shop_sort1, area_id, area_name,
							area_img, value, price, expireTime, discount,
							detailed_sort, rating, use_restriction, use_time,
							end_time, use_num, source, buy_url, photo_small,
							photo_large, is_buy, mlat, mlng, poi_id, cpsState,buy_start_time ,buy_end_time, detail);
					shops.add(shop);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		int end = shops.size();
		if (result != null) {
			if (end % 8 != 0 || end == start) {
				result.setPage(false);
			} else {
				result.setPage(true);
			}
		}
		return result;
	}

	public static MResult getHotWebsite(List<Website> websites, String city) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.hot_partner));
		try {
			sb.append("&city=").append(URLEncoder.encode(city, "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		MResult result = HttpUtil.GET(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String name = (String) item.getString("name");
					String count = (String) item.getString("count");
					Website website = new Website(id, name, count);
					websites.add(website);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}

	public static MResult getHotCity(List<CityTag> cityTags) {
		MResult result = HttpUtil.GET(API.getAPI(API.hot_city));
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					int id = item.getInt("id");
					String name = item.getString("name");
					CityTag cityTag = new CityTag(id, name);
					cityTags.add(cityTag);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}
	public static MResult getHotWord(List<String> words) {
		MResult mResult = HttpUtil.GET(API.getAPI(API.hot_keyword));
		try {
			if (mResult != null && mResult.isSuccess()) {
				String str_names  = mResult.data;
				String names[] =  str_names.substring(2, str_names.length()-2).split("\",\"");
				for (int i = 0; i < names.length; i++) {
					words.add(names[i]);
				}
			}
		} catch (Exception e) {
			words.add("郊游");
			words.add("热门电影");
			words.add("美甲");
			words.add("自助餐");
			words.add("免费停车位");
			words.add("美容");
			words.add("踏青");
			words.add("汽车保养");
			mResult.status=1;
			e.printStackTrace();
		}
		return mResult;
		
	}

	public static MResult getShopsFromAround(List<Shop> shops, String lat,
			String lng, int page, String cat, String area, String city,
			String mDistance, int sort, int order) {

		int start = shops.size();
		StringBuffer sb = new StringBuffer(API.getAPI(API.coupon_list));
		try {
			sb.append("&gz=").append(1);
			sb.append("&lng=").append(lng);
			sb.append("&lat=").append(lat);
			sb.append("&k=").append(mDistance);
			sb.append("&sort=").append(sort);
			sb.append("&order=").append(order);
			sb.append("&cat=").append(cat);
			sb.append("&area=").append(area);
			sb.append("&page=").append(page);
			sb.append("&offset=").append("8");
			sb.append("&city=").append(URLEncoder.encode(city, "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String title = (String) item.getString("title");
					String info = (String) item.getString("info");
					String sort1 = (String) item.getString("sort");
					String distance = (String) item.getString("distance");
					String shop_id = (String) item.getString("shop_id");
					String shop_title = (String) item.getString("shop_title");
					String shop_address = (String) item
							.getString("shop_address");
					String shop_tel = (String) item.getString("shop_tel");
					String shop_phone = (String) item.getString("shop_phone");
					String shop_sort1 = (String) item.getString("shop_sort");
					String area_id = (String) item.getString("area_id");
					String area_name = (String) item.getString("area_name");
					String area_img = (String) item.getString("area_img");

					String value = (String) item.getString("value");
					String price = (String) item.getString("price");
					String expireTime = (String) item.getString("expireTime");
					String discount = (String) item.getString("discount");
					String detailed_sort = (String) item
							.getString("detailed_sort");
					String rating = (String) item.getString("rating");
					String use_restriction = (String) item
							.getString("use_restriction");
					String use_time = (String) item.getString("use_time");
					String end_time = (String) item.getString("end_time");
					String use_num = (String) item.getString("use_num");
					String source = (String) item.getString("source");
					String buy_url = (String) item.getString("buy_url");
					String photo_small = (String) item.getString("photo_small");
					String photo_large = (String) item.getString("photo_large");
					String is_buy = (String) item.getString("is_buy");
					String mlat = (String) item.getString("latitude");
					String mlng = (String) item.getString("longitude");
					String poi_id = (String) item.getString("poi_id");
					int buy_start_time = (int)item.getInt("buy_start_time");
					int buy_end_time = (int)item.getInt("buy_end_time");
					int cpsState = (int) item.getInt("is_cps");
					String detail = (String) item.getString("tuan_detail");
					Shop shop = new Shop(id, title, info, sort1, distance,
							shop_id, shop_title, shop_address, shop_tel,
							shop_phone, shop_sort1, area_id, area_name,
							area_img, value, price, expireTime, discount,
							detailed_sort, rating, use_restriction, use_time,
							end_time, use_num, source, buy_url, photo_small,
							photo_large, is_buy, mlat, mlng, poi_id, cpsState,buy_start_time,buy_end_time,detail);
					shops.add(shop);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		int end = shops.size();
		if (result != null) {
			if ((end-start) < 8) {
				result.setPage(false);
			} else {
				result.setPage(true);
			}
		}
		return result;
	}

	public static MResult getShopsFromHotWebsite(List<Shop> shops, String cat,
			String website_id, int page, String city, int sort, int order) {

		int start = shops.size();
		StringBuffer sb = new StringBuffer(API.getAPI(API.hot_partner));
		try {
			sb.append("&cat=").append(cat);
			sb.append("&p=").append(website_id);
			sb.append("&sort=").append(sort);
			sb.append("&order=").append(order);
			sb.append("&page=").append(page);
			sb.append("&offset=").append("8");
			sb.append("&gz=1");
			sb.append("&city=").append(URLEncoder.encode(city, "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String title = (String) item.getString("title");
					String info = (String) item.getString("info");
					String sort1 = (String) item.getString("sort");
					String distance = (String) item.getString("distance");
					String shop_id = (String) item.getString("shop_id");
					String shop_title = (String) item.getString("shop_title");
					String shop_address = (String) item
							.getString("shop_address");
					String shop_tel = (String) item.getString("shop_tel");
					String shop_phone = (String) item.getString("shop_phone");
					String shop_sort1 = (String) item.getString("shop_sort");
					String area_id = (String) item.getString("area_id");
					String area_name = (String) item.getString("area_name");
					String area_img = (String) item.getString("area_img");

					String value = (String) item.getString("value");
					String price = (String) item.getString("price");
					String expireTime = (String) item.getString("expireTime");
					String discount = (String) item.getString("discount");
					String detailed_sort = (String) item
							.getString("detailed_sort");
					String rating = (String) item.getString("rating");
					String use_restriction = (String) item
							.getString("use_restriction");
					String use_time = (String) item.getString("use_time");
					String end_time = (String) item.getString("end_time");
					String use_num = (String) item.getString("use_num");
					String source = (String) item.getString("source");
					String buy_url = (String) item.getString("buy_url");
					String photo_small = (String) item.getString("photo_small");
					String photo_large = (String) item.getString("photo_large");
					String is_buy = (String) item.getString("is_buy");
					String mlat = (String) item.getString("latitude");
					String mlng = (String) item.getString("longitude");
					String poi_id = (String) item.getString("poi_id");
					int cpsState = (int) item.getInt("is_cps");
					int buy_start_time = (int)item.getInt("buy_start_time");
					int buy_end_time = (int)item.getInt("buy_end_time");
					String detail = (String) item.getString("tuan_detail");
					Shop shop = new Shop(id, title, info, sort1, distance,
							shop_id, shop_title, shop_address, shop_tel,
							shop_phone, shop_sort1, area_id, area_name,
							area_img, value, price, expireTime, discount,
							detailed_sort, rating, use_restriction, use_time,
							end_time, use_num, source, buy_url, photo_small,
							photo_large, is_buy, mlat, mlng, poi_id, cpsState ,buy_start_time ,buy_end_time, detail);
					shops.add(shop);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		int end = shops.size();
		if (result != null) {
			if ((end-start) < 8) {
				result.setPage(false);
			} else {
				result.setPage(true);
			}
		}
		return result;
	}

	public static MResult getShopsFromToday(List<Shop> shops, String cat,
			int page, String city, int sort, int order) {

		int start = shops.size();
		StringBuffer sb = new StringBuffer(API.getAPI(API.today_coupon));
		try {
			sb.append("&gz=").append(1);
			sb.append("&cat=").append(cat);
			sb.append("&sort=").append(sort);
			sb.append("&order=").append(order);
			sb.append("&page=").append(page);
			sb.append("&offset=").append(8);
			sb.append("&city=").append(URLEncoder.encode(city, "utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String title = (String) item.getString("title");
					String info = (String) item.getString("info");
					String sort1 = (String) item.getString("sort");
					String distance = (String) item.getString("distance");
					String shop_id = (String) item.getString("shop_id");
					String shop_title = (String) item.getString("shop_title");
					String shop_address = (String) item
							.getString("shop_address");
					String shop_tel = (String) item.getString("shop_tel");
					String shop_phone = (String) item.getString("shop_phone");
					String shop_sort1 = (String) item.getString("shop_sort");
					String area_id = (String) item.getString("area_id");
					String area_name = (String) item.getString("area_name");
					String area_img = (String) item.getString("area_img");

					String value = (String) item.getString("value");
					String price = (String) item.getString("price");
					String expireTime = (String) item.getString("expireTime");
					String discount = (String) item.getString("discount");
					String detailed_sort = (String) item
							.getString("detailed_sort");
					String rating = (String) item.getString("rating");
					String use_restriction = (String) item
							.getString("use_restriction");
					String use_time = (String) item.getString("use_time");
					String end_time = (String) item.getString("end_time");
					String use_num = (String) item.getString("use_num");
					String source = (String) item.getString("source");
					String buy_url = (String) item.getString("buy_url");
					String photo_small = (String) item.getString("photo_small");
					String photo_large = (String) item.getString("photo_large");
					String is_buy = (String) item.getString("is_buy");
					String mlat = (String) item.getString("latitude");
					String mlng = (String) item.getString("longitude");
					String poi_id = (String) item.getString("poi_id");
					int cpsState = (int) item.getInt("is_cps");
					int buy_start_time = (int)item.getInt("buy_start_time");
					int buy_end_time = (int)item.getInt("buy_end_time");
					String detail = (String) item.getString("tuan_detail");
					Shop shop = new Shop(id, title, info, sort1, distance,
							shop_id, shop_title, shop_address, shop_tel,
							shop_phone, shop_sort1, area_id, area_name,
							area_img, value, price, expireTime, discount,
							detailed_sort, rating, use_restriction, use_time,
							end_time, use_num, source, buy_url, photo_small,
							photo_large, is_buy, mlat, mlng, poi_id, cpsState, buy_start_time, buy_end_time, detail);
					shops.add(shop);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		int end = shops.size();
		if (result != null) {
			if ((end-start) < 8) {
				result.setPage(false);
			} else {
				result.setPage(true);
			}
		}
		return result;
	}

	// 首次支付
	public static MRsa first_pay(String uid, String session, String shopId,
			String size, String mobile, String receiver_name,
			String receiver_address, String leave_msg, String mCouponCode) {
		MRsa mRsa = null;
		try {
			receiver_name = URLEncoder.encode(receiver_name, "utf-8");
			receiver_address = URLEncoder.encode(receiver_address, "utf-8");
			leave_msg = URLEncoder.encode(leave_msg, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}

		StringBuffer sb = new StringBuffer(API.getAPI(API.first_pay));
		sb.append("&uid=").append(uid);
		sb.append("&mobile_session=").append(session);
		sb.append("&coupon_id=").append(shopId);
		sb.append("&mobile=").append(mobile);
		sb.append("&quantity=").append(size);
		sb.append("&receiver=").append(receiver_name);
		sb.append("&street=").append(receiver_address);
		sb.append("&ps=").append(leave_msg);
		if (!"".equals(mCouponCode)) {
			sb.append("&code=").append(mCouponCode);
		}
		try {
			MResult result = HttpUtil.GET(sb.toString());
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String orderinfo = (String) item.getString("orderData");
					String sign = (String) item.getString("sign");
					String sign_type = (String) item.getString("sign_type");
					String mUid = (String) item.getString("uid");
					String mSession = (String) item.getString("session_key");
					String isTmp = (String) item.getString("is_tmp");
					sign = URLEncoder.encode(sign);
					String info = orderinfo + "&sign=\"" + sign
							+ "\"&sign_type=\"" + sign_type + "\"";
					mRsa = new MRsa(info, mUid, mSession, isTmp);
					return mRsa;
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mRsa;
	}

	// 重复支付
	public static MResult agian_pay(String uid, String session, String orderId) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.agian_pay));
		sb.append("&gz=").append(1);
		sb.append("&uid=").append(uid);
		sb.append("&mobile_session=").append(session);
		sb.append("&orderId=").append(orderId);
		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String orderinfo = (String) item.getString("orderData");
					String sign = (String) item.getString("sign");
					String sign_type = (String) item.getString("sign_type");
					sign = URLEncoder.encode(sign);
					String info = orderinfo + "&sign=\"" + sign
							+ "\"&sign_type=\"" + sign_type + "\"";
					result.setInfo(info);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static MResult getOrders(List<Order> orders, String uid,
			String sessionId, int page, int orderstatus) {
		if (page == 1) {
			orders.clear();
		}
		int start = orders.size();
		StringBuffer sb = new StringBuffer(API.getAPI(API.my_order));
		sb.append("&uid=").append(uid);
		sb.append("&mobile_session=").append(sessionId);
		sb.append("&page=").append(page);
		sb.append("&offset=").append("8");
		sb.append("&t=").append(orderstatus);
		sb.append("&gz=1");
		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray(result.data);
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String order_sn = (String) item.getString("order_sn");
					String tuan_site = (String) item.getString("tuan_site");
					String buy_time = (String) item.getString("buy_time");
					String pay_status = (String) item.getString("pay_status");
					String total_price = (String) item.getString("total_price");
					String expire_time = (String) item.getString("expire_time");
					String tuan_title = (String) item.getString("tuan_title");
					String address = "";
					if (item.has("address")) {
						address = (String) item.getString("address");
					}
					String image = (String) item.getString("image");
					String is_buy = (String) item.getString("is_buy");
					String tuan_id = (String) item.getString("tuan_id");
					String quantity = (String) item.getString("quantity");
					String price = (String) item.getString("price");
					String code_sn = (String) item.getString("code_sn");
					String code_status = (String) item.getString("code_status");
					String code_money = (String) item.getString("code_money");
					String code_errmsg = (String) item.getString("code_errmsg");
					int buy_start_time = (int) item.getInt("buy_start_time");
					int buy_end_time = (int) item.getInt("buy_end_time");
					int cpsState = 0;
					if (item.has("is_cps")) {
						cpsState = (int) item.getInt("is_cps");
					}
					Order order = new Order(id, order_sn, tuan_site, buy_time,
							pay_status, total_price, expire_time, tuan_title,
							address, image, is_buy, tuan_id, quantity, price,
							cpsState, code_sn, code_status, code_money,
							code_errmsg, buy_start_time, buy_end_time);
					orders.add(order);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();

		}
		int end = orders.size();
		if (result != null) {
			if (end % 8 != 0 || end == start) {
				result.setPage(false);
			} else {
				result.setPage(true);
			}
		}
		return result;
	}

	public static MResult getShop_detail(Shop shop, String coupon_id) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.shop_detail));
		sb.append("&gz=").append(1);
		sb.append("&cid=").append(coupon_id);

		MResult result = HttpUtil.GET_COMPRESS(sb.toString());
		if (result != null && result.isSuccess()) {
			JSONArray json;
			try {
				json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String title = (String) item.getString("title");
					String info = (String) item.getString("info");
					String sort1 = (String) item.getString("sort");
					String distance = "";
					String shop_id = "";
					if (item.has("shop_id")) {
						shop_id = (String) item.getString("shop_id");
					}
					String shop_title = "";
					if (item.has("shop_title")) {
						shop_title = (String) item.getString("shop_title");

					}
					String shop_address = (String) item
							.getString("shop_address");
					String shop_tel = (String) item.getString("shop_tel");
					String shop_phone = (String) item.getString("shop_phone");
					String shop_sort1 = (String) item.getString("shop_sort");
					String area_id = (String) item.getString("area_id");
					String area_name = (String) item.getString("area_name");
					String area_img = (String) item.getString("area_img");

					String value = (String) item.getString("value");
					String price = (String) item.getString("price");
					String expireTime = (String) item.getString("expireTime");
					String discount = (String) item.getString("discount");
					String detailed_sort = "";
					String rating = (String) item.getString("rating");
					String use_restriction = (String) item
							.getString("use_restriction");
					String use_time = (String) item.getString("use_time");
					String end_time = (String) item.getString("end_time");
					String use_num = "";
					String source = (String) item.getString("source");
					String buy_url = (String) item.getString("buy_url");
					String photo_small = (String) item.getString("photo_small");
					String photo_large = (String) item.getString("photo_large");
					String is_buy = (String) item.getString("is_buy");
					String mlat = (String) item.getString("latitude");
					String mlng = (String) item.getString("longitude");
					String poi_id = (String) item.getString("poi_id");
					String flower_language = "";
					int buy_start_time = (int) item.getInt("buy_start_time");
					int buy_end_time = (int) item.getInt("buy_end_time");
					int cpsState = (int) item.getInt("is_cps");
					shop.setId(id);
					shop.setTitle(title);
					shop.setInfo(info);
					shop.setSort(sort1);
					shop.setDistance(distance);
					shop.setShop_id(shop_id);
					shop.setShop_title(shop_title);
					shop.setShop_address(shop_address);
					shop.setShop_tel(shop_tel);
					shop.setShop_phone(shop_phone);
					shop.setShop_sort(shop_sort1);
					shop.setArea_id(area_id);
					shop.setShop_sort(shop_sort1);
					shop.setArea_id(area_id);
					shop.setArea_name(area_name);
					shop.setArea_img(area_img);
					shop.setValue(value);
					shop.setPrice(price);
					shop.setExpireTime(expireTime);
					shop.setDiscount(discount);
					shop.setDetailed_sort(detailed_sort);
					shop.setRating(rating);
					shop.setUse_restriction(use_restriction);
					shop.setUse_time(use_time);
					shop.setEnd_time(end_time);
					shop.setUse_num(use_num);
					shop.setSource(source);
					shop.setBuy_url(buy_url);
					shop.setPhoto_small(photo_small);
					shop.setPhoto_large(photo_large);
					shop.setIs_buy(is_buy);
					shop.setLat(mlat);
					shop.setLng(mlng);
					shop.setPoi_id(poi_id);
					shop.setCpsState(cpsState);
					shop.setBuy_start_time(buy_start_time);
					shop.setBuy_end_time(buy_end_time);

				}
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * @author 董永健 意见提交
	 * @param content
	 *            建议内容
	 * @param user_id
	 *            用户ID
	 * @return
	 * @param
	 */
	public static MResult setPropose(String content, String user_id,
			String mobile_session) {

		try {
			content = URLEncoder.encode(content, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer(API.getAPI(API.propose));
		sb.append("&content=").append(content);
		sb.append("&user_id=").append(user_id);
		sb.append("&mobile_session=").append(mobile_session);
		MResult result = HttpUtil.GET(sb.toString());
		return result;
	}

	/**
	 * @author 董永健 日志提交
	 * @param content
	 *            建议内容
	 * @param user_id
	 *            用户ID
	 * @return
	 * @param
	 */
	public static MResult setLog(String content, String user_id,
			String mobile_session) {

		try {
			content = URLEncoder.encode(content, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer(API.getAPI(API.phone_log));
		sb.append("&content=").append(content);
		sb.append("&user_id=").append(user_id);
		sb.append("&mobile_session=").append(mobile_session);
		return HttpUtil.GET(sb.toString());
	}

	public static MResult login(User mUser, String inputString,
			String password, String session, String uid) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.login));
		sb.append("&email=").append(inputString);
		sb.append("&password=").append(password);
		sb.append("&session=").append(session);
		sb.append("&uid=").append(uid);
		MResult result = HttpUtil.GET(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String nickname = (String) item.getString("nickname");
					String sessionKey = (String) item.getString("sessionKey");
					String isTmp = item.getString("is_tmp");
					mUser.setId(id);
					mUser.setNickname(nickname);
					mUser.setSessionKey(sessionKey);
					mUser.setErrorCode(result.getErrorCode());
					mUser.setIsTmp(isTmp);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static MResult register(User mUser, String mAccount, String mPasswd,
			String session_id, String uid) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.register));
		sb.append("&password=").append(mPasswd);
		sb.append("&login_name=").append(mAccount);
		sb.append("&mobile_session=").append(session_id);
		sb.append("&uid=").append(uid);
		MResult result = HttpUtil.GET(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("uid");
					String nickname = (String) item.getString("nickname");
					String sessionKey = (String) item.getString("sessionKey");
					String isTmp = item.getString("is_tmp");

					mUser.setId(id);
					mUser.setNickname(nickname);
					mUser.setSessionKey(sessionKey);
					mUser.setErrorCode(result.getErrorCode());
					mUser.setIsTmp(isTmp);
				}
			} else {
				mUser = new User(result.getErrorCode());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static MResult register_weibo(User user, String oauth_token,
			String oauth_token_secret, String source, String session_id,
			String uid) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.weibo_login));
		sb.append("&oauth_token=").append(oauth_token);
		sb.append("&oauth_token_secret=").append(oauth_token_secret);
		sb.append("&source=").append(source);
		sb.append("&mobile_session=").append(session_id);
		sb.append("&uid=").append(uid);

		MResult result = HttpUtil.GET(sb.toString());
		try {
			if (result != null && result.isSuccess()) {
				JSONArray json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					String id = (String) item.getString("id");
					String nickName = (String) item.getString("nickname");
					String sessionKey = (String) item.getString("sessionKey");
					String isTmp = item.getString("is_tmp");
					user.setId(id);
					user.setNickname(nickName);
					user.setSessionKey(sessionKey);
					user.setErrorCode(result.getErrorCode());
					user.setIsTmp(isTmp);
				}
			} else {
				user.setErrorCode(result.getErrorCode());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 版本检测
	public static MResult checkVersion() {
		return HttpUtil.GET(API.getAPI(API.version));
	}

	public static MResult cancel_order(String orderId, String uid,
			String session) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.cancel_order));
		sb.append("&orderId=").append(orderId);
		sb.append("&uid=").append(uid);
		sb.append("&mobile_session=").append(session);
		return HttpUtil.GET(sb.toString());
	}

	// 验证优惠码是否 有效
	public static MResult confirm_coupon_code(String code, String quantity,
			String couponId) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.confirm_coupon_code));
		sb.append("&code=").append(code);
		sb.append("&quantity=").append(quantity);
		sb.append("&coupon_id=").append(couponId);
		return HttpUtil.GET(sb.toString());

	}

	public static MResult setHotPlace(CityTag cityTag, String lat, String lng,
			String city) {
		try {
			city = URLEncoder.encode(city, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer(API.getAPI(API.hot_place));
		sb.append("&lat=").append(lat);
		sb.append("&lng=").append(lng);
		sb.append("&city=").append(city);
		MResult result = HttpUtil.GET(sb.toString());
		if (result != null && result.isSuccess()) {
			if (!"".equals(result.data))
				try {
					JSONArray jsonCity = new JSONArray(result.data);
					JSONObject itemCity = jsonCity.getJSONObject(0);
					int idCity = itemCity.getInt("id");
					String tagNameCity = itemCity.getString("tag_name");
					String tagchildCity = itemCity.getString("_child");
					List<CityTag> tags = new ArrayList<CityTag>();
					List<CityTag> fistTag = new ArrayList<CityTag>();
					CityTag fistcityTagSanjak = new CityTag(0,"全部商圈",fistTag);
					tags.add(fistcityTagSanjak);
//					tags
					JSONArray jsonSanjak = new JSONArray(tagchildCity);
					for (int i = 0; i < jsonSanjak.length(); i++) {
						JSONObject itemSanjak = jsonSanjak.getJSONObject(i);
						int idSanjak = itemSanjak.getInt("id");
						String tagNameSanjak = itemSanjak.getString("tag_name");
						List<CityTag> tagsSanjak = new ArrayList<CityTag>();
						
						CityTag fistTagChild = new CityTag(idSanjak, "全部");
						tagsSanjak.add(fistTagChild);
						if (itemSanjak.has("_child")) {
							String tagchildSanjak = itemSanjak
									.getString("_child");

							try {
								JSONArray jsonChild = new JSONArray(
										tagchildSanjak);
								for (int j = 0; j < jsonChild.length(); j++) {
									JSONObject itemChild = jsonChild
											.getJSONObject(j);
									int idChild = itemChild.getInt("id");
									String tagNameChild = itemChild
											.getString("tag_name");

									CityTag cityTagChild = new CityTag(idChild,
											tagNameChild);
									tagsSanjak.add(cityTagChild);
								}

							} catch (JSONException e) {
								e.printStackTrace();
							}
							CityTag cityTagSanjak = new CityTag(idSanjak,
									tagNameSanjak, tagsSanjak);
							tags.add(cityTagSanjak);
						}
					}
					cityTag.setId(idCity);
					cityTag.setTag_name(tagNameCity);
					cityTag.setTags(tags);
				} catch (JSONException e) {
					e.printStackTrace();
				}
		}
		return result;
	}

	public static MResult geo_offset(MLocation mylocation, Location location_) {
		StringBuffer sb = new StringBuffer(API.getAPI(API.geo_offset));
		sb.append("&lat=").append(location_.getLatitude());
		sb.append("&lng=").append(location_.getLongitude());
		MResult result = HttpUtil.GET(sb.toString());
		if (result != null && result.isSuccess()) {
			JSONArray json;
			try {
				json = new JSONArray("[" + result.data + "]");
				for (int i = 0; i < json.length(); i++) {
					JSONObject item = json.getJSONObject(i);
					double lat = Double.parseDouble(item.getString("lat"));
					double lng = Double.parseDouble(item.getString("lng"));
					String city = item.getString("city");
					String address = item.getString("poi_name");
					mylocation.setLat(lat);
					mylocation.setLng(lng);
					mylocation.setCity(city);
					mylocation.setAddress(address);
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public static MResult isCps(String cid) {
		String uCps = API.getAPI(API.cps) + "cid=" + cid;
		return HttpUtil.GET(uCps);
	}

	public static String alipay_card_first(String uid, String coupon_id,
			String size, String tel, String receiver_name,
			String receiver_address, String leave_msg, String mCouponCode) {
		try {
			receiver_name = URLEncoder.encode(receiver_name, "utf-8");
			receiver_address = URLEncoder.encode(receiver_address, "utf-8");
			leave_msg = URLEncoder.encode(leave_msg, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		StringBuffer sb = new StringBuffer(API.first_card_pay);
		sb.append("&uid=").append(uid);
		sb.append("&coupon_id=").append(coupon_id);
		sb.append("&tel=").append(tel);
		sb.append("&quantity=").append(size);
		if (!"".equals(mCouponCode)) {
			sb.append("&code=").append(mCouponCode);
		}
		sb.append("&receiver=").append(receiver_name);
		sb.append("&street=").append(receiver_address);
		sb.append("&ps=").append(leave_msg);
		return sb.toString();
	}

	public static String alipay_card_again(String uid, String order_id) {
		StringBuffer sb = new StringBuffer(API.agian_card_pay);
		sb.append("&uid=").append(uid);
		sb.append("&orderId=").append(order_id);
		return sb.toString();
	}

	public static MResult getAdvertisings(List<Advertising> advs) {
		MResult result = HttpUtil.GET(API.getAPI(API.advertising));
		if (result != null && result.isSuccess()) {
			try {
				JSONArray jsonArray = new JSONArray(result.data);
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject item = jsonArray.getJSONObject(i);
					String id = (String) item.getString("id");
					String image_url = (String) item.getString("adv_url");
					String link_url = (String) item.getString("link_url");
					String link_type = (String) item.getString("link_type");
					Advertising avd = new Advertising(id, image_url, link_url,
							link_type);

					advs.add(avd);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public static MResult getPushData(Push push, String pushCity, String pushStatus, String pushid) {
		StringBuffer build = new StringBuffer(API.getAPI(API.push));
		String city ="";
		try {
			city = URLEncoder.encode(pushCity, "utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		build.append("&city=").append(city);
		build.append("&status=").append(pushStatus);
		build.append("&id=").append(pushid);
		MResult result = HttpUtil.GET(build.toString());
		if (result != null && result.isSuccess()) {
			try {
					JSONObject object=new JSONObject(result.data);
					String push_type = "";
					String title = "";
					String content = "";
					String link_url = "";
					String id = "";
					if(object.has("push_type")){
						push_type = (String) object.getString("push_type");
					}
					if(object.has("title")){
						title = (String) object.getString("title");
					}
					if(object.has("content")){
						content = (String) object.getString("content");
					}
					if(object.has("link_url")){
						link_url = (String) object.getString("link_url");
					}
					if(object.has("id")){
						id = (String) object.getString("id");
					}
					push.setPush_type(push_type);
					push.setLink_url(link_url);
					push.setTitle(title);
					push.setContent(content);
					push.setId(id);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	
}
